﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace ChessServises
{
    public class ChessService : IChessService
    {
        public int CreateGame(Guid creatorId)
        {
            ChessDAL dal = new ChessDAL();
            int tableId = dal.CreateGame(creatorId, Colour.White);

            return tableId;
        }

        public Guid CreateUser(string userName)
        {
            ChessDAL dal = new ChessDAL();
            Guid userId = dal.CreateUser(userName);

            return userId;
        }

        public Stream GetGameTable(GetTableParameters parameters)
        {
            ChessDAL dal = new ChessDAL();
            GameTable table = dal.GetTableById(parameters.TableId);
            if (table.LastModified <= parameters.LastVersion)
            {
                return null;
            }
            else
            {                
                MemoryStream stream = new MemoryStream(table.Table);
                return stream;
            }             
        }

        public void MakeMove(Move move)
        {
            ChessDAL dal = new ChessDAL();
            dal.ChangeTable(move.tableId, move.StreamNewBoard);
        }


        public List<int> GetTableIds()
        {
            ChessDAL dal = new ChessDAL();
            return dal.GetTableIds();
        }
    }   
}
